import React, { Component } from 'react';
import { Affix, Button, Form, Radio } from 'antd';
import { messages } from 'share/common';
import taxCodeServer from '../../tax-code-server';

const RadioGroup = Radio.Group;

@Form.create()
class ControlLevel extends Component {

  state = {
    formList: [{
      dataIndex: 'enable',
      label: messages('expense-14.key370')/*管控力度*/,
      component: RadioGroup,
      params: {
        options: [
          { label: messages('expense-14.key371')/*弱管控*/, value: false },
          { label: messages('expense-14.key372')/*强管控*/, value: true }
        ]
      },
      child: null
    }]
  };


  componentDidMount() {
    const { controlLevel, form: { setFieldsValue } } = this.props;
    setFieldsValue({ enable: controlLevel });
  }

  handleSave = () => {
    const { id, form: { validateFieldsAndScroll } } = this.props;
    validateFieldsAndScroll(async (errors, values) => {
      if (!errors) {
        try {
          values.audRuleExpTypeIds = [id];
          this.setState({ submitLoading: true });
          await taxCodeServer.setControlLevel(values);
          this.props.close(true);
        }catch (e) {

        }
        this.setState({ submitLoading: false });
      }
    });
  };

  handleCancel = () => {
    this.props.close();
  };

  render() {
    const { form: { getFieldDecorator }, readOnly } = this.props;
    const { formList, submitLoading } = this.state;
    const formItemLayout = { labelCol: { span: 4 }, wrapperCol: { span: 12, offset: 1 } };
    return (
      <div>
        <Form className="new-form">
          {
            formList.map(item => (
              <Form.Item label={item.label}  {...formItemLayout} >
                {
                  getFieldDecorator(item.dataIndex, {})(React.createElement(item.component, item.params, item.child))
                }
              </Form.Item>
            ))
          }
          <div className="slide-footer">
            <Button
              type="primary"
              loading={submitLoading}
              disabled={readOnly}
              htmlType="submit"
              onClick={this.handleSave}
            >
              {messages('common.save')/*保存*/}
            </Button>
            <Button
              loading={submitLoading}
              onClick={this.handleCancel}
              disabled={readOnly}
              className="new-unit-cancel"
            >
              {messages('common.cancel')/*取消*/}
            </Button>
          </div>
        </Form>
      </div>
    );
  }
}

ControlLevel.propTypes = {};

export default ControlLevel;
